#ifndef PICCOLO_APPLICATION_H
#define PICCOLO_APPLICATION_H

#include "PiccoloWindow.h"
#include "PiccoloApplicationContext.h"

#include "ECS/PScene.h"

namespace Piccolo
{
    struct AppSettings
    {
        uint32_t width = 800.f;
        uint32_t height = 600.f;
        const char* title = "Piccolo Engine";
    };

    class PiccoloApplication
    {
    public:
        void Start(int argc, char* argv[]);
        void Tick();
        void Stop();

        void Pause() { bPause = true; }
        void Resume() { if (bPause) bPause = false; }
        bool IsPaused() const { return bPause; }

        float GetStartTimeSecond() const { return std::chrono::duration<float>(std::chrono::steady_clock::now() - mStartTimePoint).count(); }
        uint64_t GetFrameIndex() const { return mFrameIndex; }

        static PiccoloAppContext* GetAppContext() { return &sAppContext; }

    protected:
        virtual void OnConfiguration(AppSettings* appSettings) {}
        virtual void OnInit() {}
        virtual void OnUpdate(float deltaTime) {}
        virtual void OnRender() {}
        virtual void OnDestroy() {}

        virtual void OnSceneInit(PScene* scene) {}
        virtual void OnSceneDestroy(PScene* scene) {}

        std::chrono::steady_clock::time_point mStartTimePoint;
        std::chrono::steady_clock::time_point mLastTimePoint;
        std::shared_ptr<PiccoloRenderContext> mRenderContext;
        std::unique_ptr<PiccoloWindow> mWindow;

    private:
        void ParseArgs(int argc, char* argv[]);
        bool LoadScene(const std::string& filePath = "");
        void UnloadScene();

        std::unique_ptr<PScene> mScene;
        AppSettings mAppSettings;

        uint64_t mFrameIndex = 0;
        bool bPause = false;

        static PiccoloAppContext sAppContext;
    };
}

#endif